<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/5/20
 * Time: 16:16
 */
namespace app\admin\controller;
use app\models\Models;

/**
 * +-------------------------------------------
 * Class Rule
 * @package app\admin\controller
 * 权限规则增删改查
 * +-------------------------------------------
 */
class Rule extends Check{
    /**
     * @return \think\response\View
     * 权限列表
     */
    public function ruleList(){
        $get    = input('param.');
        $rule   = new Models('auth_rule');
        $search = search($get,'title');    // 搜索
        $where  = $search['condition'];
        $list   = $rule->_select($where);
        if(!$get){
            $list = infinite($list,0,0);
        }
        return view('ruleList',['list'=>$list,'display'=>$search['display']]);
    }

    /**
     * 添加权限规则
     * @return array|\think\response\View
     */
    public function ruleAdd(){
        $rule = new Models('auth_rule');
        if(request()->isPost()){
            $post = input('param.');
            try{
                // 场景验证
                $validate = self::validate($post,'Rule.ruleAdd');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                // 添加权限
                $bool = $rule->_add($post);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
                // 添加操作日志
                $log  = self::addLog('添加权限-'.$post['title']);
                if(!$log){
                    throw new \LogicException('操作失败',1010);
                }
            }catch(\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $parent_rule = $rule->_select(['p_id'=>0],"id,title");//获取顶级栏目的数据
        return view('ruleAdd',['parent_rule'=>$parent_rule]);
    }

    /**
     * 更新权限规则
     * @return array|\think\response\View
     */
    public function ruleSave(){
        $rule = new Models('auth_rule');
        if(request()->isPost()){
            $post = input('param.');
            try{
                // 场景验证
                $validate = self::validate($post,'Rule.ruleAdd');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                // 修改权限
                $bool = $rule->_save($post);
                if(false === $bool){
                    throw new \LogicException('操作失败',1010);
                }
                // 添加操作日志
                $log  = self::addLog('修改权限-'.$post['title']);
                if(!$log){
                    throw new \LogicException('操作失败',1010);
                }
            }catch(\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $get  = input('param.');
        $info = $rule->_find($get['id']);
        // 获取顶级栏目的数据
        $list = $rule->_select(['p_id'=>0],"id,title");
        return view('ruleSave',['parent_rule'=>$list,'info'=>$info]);
    }
}